home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / BASICS.ZIP / CLASSLIB / CCLIENT.CPP < prev    next >
C/C++ Source or Header  |  1993-06-07  |  17KB  |  710 lines

  1. /*
  2.  * CCLIENT.CPP
  3.  * Sample Code Class Libraries
  4.  *
  5.  * Implementation of the CClient class that handles an SDI or MDI
  6.  * client area window.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18.  
  19. #include <windows.h>
  20. #include "classlib.h"
  21.  
  22.  
  23.  
  24. /*
  25.  * CClient::CClient
  26.  * CClient::~CClient
  27.  *
  28.  * Constructor Parameters:
  29.  *  hInst           HINSTANCE of the application.
  30.  */
  31.  
  32. CClient::CClient(HINSTANCE hInst)
  33.     : CWindow(hInst)
  34.     {
  35.     return;
  36.     }
  37.  
  38.  
  39. CClient::~CClient(void)
  40.     {
  41.     return;
  42.     }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. /*
  51.  * CClient::FInit
  52.  *
  53.  * Purpose:
  54.  *  Creates a client area window sensitive to SDI or MDI (compile-time).
  55.  *
  56.  * Parameters:
  57.  *  hMenuWindow     HWND of the Window menu on the frame.
  58.  *  pRect           LPRECT containing the desired window rectangle.
  59.  *  pFR             LPCFrame to which this client belongs.
  60.  *
  61.  * Return Value:
  62.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  63.  */
  64.  
  65. BOOL CClient::FInit(HMENU hMenuWindow, LPRECT pRect, LPCFrame pFR)
  66.     {
  67.     HWND                hWndFrame;
  68.    #ifdef MDI
  69.     CLIENTCREATESTRUCT  ccs;
  70.    #endif
  71.  
  72.     m_pFR=pFR;
  73.  
  74.     hWndFrame=m_pFR->Window();
  75.  
  76.    #ifdef MDI
  77.  
  78.     ccs.hWindowMenu =hMenuWindow;
  79.     ccs.idFirstChild=ID_MDICHILDMIN;
  80.  
  81.     //Create the MDI client filling the client area
  82.     m_hWnd=CreateWindow("mdiclient", "mdiclient"
  83.         , MDIS_ALLCHILDSTYLES | WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE
  84.         , pRect->left, pRect->top, pRect->right-pRect->left
  85.         , pRect->bottom-pRect->top, hWndFrame, NULL, m_hInst, (LPSTR)&ccs);
  86.  
  87.    #else
  88.  
  89.     //SDI case, create out own SDI Client window
  90.     m_hWnd=CreateWindow(SZCLASSSDICLIENT, SZCLASSSDICLIENT
  91.         , WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE, pRect->left, pRect->top
  92.         , pRect->right-pRect->left, pRect->bottom-pRect->top, hWndFrame
  93.         , NULL, m_hInst, 0L);
  94.  
  95.    #endif
  96.  
  97.     return (NULL!=m_hWnd);
  98.     }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. /*
  105.  * CClient::CreateCDocument
  106.  *
  107.  * Purpose:
  108.  *  Constructs a new document.  This function is overridable so an
  109.  *  app can use the default CClient but create a derived document class.
  110.  *
  111.  * Parameters:
  112.  *  None
  113.  *
  114.  * Return Value:
  115.  *  LPCDocument     Pointer to the new document object.
  116.  */
  117.  
  118. LPCDocument CClient::CreateCDocument(void)
  119.     {
  120.     return new CDocument(m_hInst);
  121.     }
  122.  
  123.  
  124.  
  125.  
  126.  
  127. /*
  128.  * CClient::TranslateAccelerator
  129.  *
  130.  * Purpose:
  131.  *  Provides an isolated system accelerator translation as necessary
  132.  *  for the client window.  In MDI this calls TranslateMDISysAccel
  133.  *  but is a NOP in SDI.
  134.  *
  135.  * Parameters:
  136.  *  pMsg            LPMSG to the message from GetMessage
  137.  *
  138.  * Return Value:
  139.  *  BOOL            TRUE if the accelerator was translated and processed,
  140.  *                  FALSE otherwise.
  141.  */
  142.  
  143. BOOL CClient::TranslateAccelerator(LPMSG pMsg)
  144.     {
  145.    #ifdef MDI
  146.     return TranslateMDISysAccel(m_hWnd, pMsg);
  147.    #else
  148.     return FALSE;
  149.    #endif
  150.     }
  151.  
  152.  
  153.  
  154.  
  155.  
  156. /*
  157.  * CClient::DefFrameProc
  158.  *
  159.  * Purpose:
  160.  *  Encapsulates which default message procedure to call for a frame window:
  161.  *  SDI (DefWindowProc) or MDI (DefFrameProc).
  162.  *
  163.  * Parameters:
  164.  *  hWnd            HWND of the frame
  165.  *  iMsg            UINT of the message
  166.  *  wParam          WPARAM of the message
  167.  *  lParam          LPARAM of the message
  168.  *
  169.  * Return Value:
  170.  *  LRESULT         Return value for the message.
  171.  */
  172.  
  173. LRESULT CClient::DefaultFrameProc(HWND hWnd, UINT iMsg, WPARAM wParam
  174.     , LPARAM lParam)
  175.     {
  176.    #ifdef MDI
  177.     return (DefFrameProc(hWnd, m_hWnd, iMsg, wParam, lParam));
  178.    #else
  179.     return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  180.    #endif
  181.     }
  182.  
  183.  
  184.  
  185.  
  186.  
  187. /*
  188.  * CClient::OnWindowCommand
  189.  *
  190.  * Purpose:
  191.  *  Handles Window menu commands for MDI situations.  This is a NOP in SDI.
  192.  *
  193.  * Parameters:
  194.  *  uCommand        UINT command to execute:
  195.  *                      WM_MDICASCADE
  196.  *                      WM_MDITILE
  197.  *                      WM_MDIICONARRANGE
  198.  *
  199.  *  uOption         UINT optional parameter for WM_MDITILE, either
  200.  *                  MDITILE_HORIZONTAL or MDITILE_VERTICAL.
  201.  *
  202.  * Return Value:
  203.  *  None
  204.  */
  205.  
  206. void CClient::OnWindowCommand(UINT uCommand, UINT uOption)
  207.     {
  208.     SendMessage(m_hWnd, uCommand, (WPARAM)uOption, 0L);
  209.     return;
  210.     }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. /*
  219.  * CClient::OnSize
  220.  *
  221.  * Purpose:
  222.  *  Handles resizing the client window when the frame is resized.
  223.  *
  224.  * Parameters:
  225.  *  x, y            UINT new location of the window.
  226.  *  cx, cy          UINT new dimensions of the window.
  227.  *
  228.  * Return Value:
  229.  *  None
  230.  */
  231.  
  232. void CClient::OnSize(UINT x, UINT y, UINT cx, UINT cy)
  233.     {
  234.     SetWindowPos(m_hWnd, NULL, x, y, cx, cy, SWP_NOZORDER | SWP_NOACTIVATE);
  235.     return;
  236.     }
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243. /*
  244.  * CClient::NewDocument
  245.  *
  246.  * Purpose:
  247.  *  Creates a new blank document in the client space.  See CloseDocument
  248.  *  for the opposite effect.
  249.  *
  250.  * Parameters:
  251.  *  fVisible        BOOL indicating if the document is to be visible or not.
  252.  *  pAdv            LPCDocumentAdviseSink to set with the new document for
  253.  *                  notifications.  Can be NULL.
  254.  *
  255.  * Return Value:
  256.  *  LPCDocument     Pointer to the new document object.
  257.  */
  258.  
  259. LPCDocument CClient::NewDocument(BOOL fVisible, LPCDocumentAdviseSink pAdv)
  260.     {
  261.     MDICREATESTRUCT mcs;
  262.     HWND            hWndDoc;
  263.     LPCDocument     pDoc;
  264.     DOCUMENTINIT    di;
  265.     BOOL            fCreate=TRUE;
  266.  
  267.    #ifdef MDI
  268.     //In MDI we create a new CDocument
  269.     pDoc=CreateCDocument();  //This could create a derived class...
  270.    #else
  271.     //In SDI we close the one we have and create a new one.
  272.     pDoc=ActiveDocument();
  273.  
  274.     if (NULL!=pDoc)
  275.         CloseDocument(pDoc);
  276.  
  277.     pDoc=CreateCDocument();
  278.    #endif
  279.  
  280.     if (fCreate)
  281.         {
  282.         /*
  283.          * We implement this by having the client window actually create the
  284.          * windows instead of using the CDocument initializer.  Reason being
  285.          * is that we ask the client to actually create the window using
  286.          * WM_MDICREATE (which works for our SDI client as well.  Since we
  287.          * need to conditionally compile for MDI or SDI here, we create
  288.          * a window before calling the document constructor.
  289.          */
  290.  
  291.         mcs.szTitle="";
  292.         mcs.szClass=SZCLASSDOCUMENT;
  293.         mcs.hOwner =m_hInst;
  294.         mcs.lParam =(LPARAM)(LPSTR)pDoc;
  295.  
  296.         mcs.x =CW_USEDEFAULT;
  297.         mcs.cx=CW_USEDEFAULT;
  298.         mcs.y =CW_USEDEFAULT;
  299.         mcs.cy=CW_USEDEFAULT;
  300.  
  301.         /*
  302.          * Set the style of the window, controlling visiblity.
  303.          * WS_CLIPCHILDREN is important to prevent unnecessary flashing
  304.          * of document contents that we'll usually fill with some
  305.          * editor window.
  306.          */
  307.        #ifdef MDI
  308.         mcs.style=WS_CHILD | WS_SYSMENU | WS_CAPTION | WS_CLIPSIBLINGS
  309.                   | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
  310.                   | WS_CLIPCHILDREN | ((fVisible) ? WS_VISIBLE : 0L);
  311.        #else
  312.         mcs.style=WS_CHILD | WS_CLIPCHILDREN | ((fVisible) ? WS_VISIBLE : 0L);
  313.        #endif
  314.  
  315.         //Tell the Client window to create the child
  316.         hWndDoc=(HWND)(UINT)SendMessage(m_hWnd, WM_MDICREATE, 0
  317.             , (LONG)(LPMDICREATESTRUCT)&mcs);
  318.  
  319.  
  320.         di.idsMin=IDS_STANDARDDOCMIN;
  321.         di.idsMax=IDS_STANDARDDOCMAX;
  322.         di.hWndDoc=hWndDoc;
  323.         di.pAdv=pAdv;
  324.  
  325.         if (!pDoc->FInit(&di))
  326.             {
  327.             SendMessage(m_hWnd, WM_MDIDESTROY, (WPARAM)hWndDoc, 0L);
  328.             delete pDoc;
  329.             return NULL;
  330.             }
  331.         }
  332.  
  333.     m_cDoc++;
  334.  
  335.     //Update menus and gizmos for the new document if visible
  336.     if (fVisible)
  337.         m_pFR->UpdateGizmos();
  338.  
  339.     return pDoc;
  340.     }
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348. /*
  349.  * CClient::ActiveDocument
  350.  *
  351.  * Purpose:
  352.  *  Returns the active document window (encapsulates WM_MDIGETACTIVE)
  353.  *
  354.  * Parameters:
  355.  *  None
  356.  *
  357.  * R